package com.tenglong.teacher.ui.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tenglong.teacher.data.requestInfo.WYLoginInfo;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.data.resultInfo.SeCourse;

import java.util.List;

/**
 * @author hdx on 2019/3/10.
 */
public class SharedPreferencesUtil {

    public static void initData(Context context){
        SharedPreferences settings = context.getSharedPreferences("preference", 0);
        ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
        DataState.getInstance().setLoginState(settings.getBoolean("loginState", false));
        if (DataState.getInstance().isLoginState()){
            userInfo.setId(settings.getString("id",""));
            userInfo.setEnable(settings.getInt("enable",0));
            userInfo.setRemark(settings.getString("remark",""));
            userInfo.setCreateBy(settings.getString("createBy",""));
            userInfo.setCreateTime(settings.getLong("createTime",0));
            userInfo.setUpdateBy(settings.getString("updateBy",""));
            userInfo.setUpdateTime(settings.getLong("updateTime",0));
            userInfo.setKeyword(settings.getString("keyword",""));
            userInfo.setUserName(settings.getString("userName",""));
            userInfo.setPhone(settings.getString("phone",""));
            userInfo.setAddress(settings.getString("address",""));
            userInfo.setToken(settings.getString("token",""));
            userInfo.setSlogan(settings.getString("slogan",""));
            userInfo.setImUserName(settings.getString("imUserName",""));
            userInfo.setImUserPassword(settings.getString("imUserPassword",""));
            userInfo.setPhotoUrl(settings.getString("photoUrl",""));
            userInfo.setSex(settings.getInt("sex",1));
        }
    }


    public static void loginIn(Context context,ClientUserInfo userInfo){
        SharedPreferences settings = context.getSharedPreferences("preference", 0);
        DataState.getInstance().setLoginState(true);
        DataState.getInstance().setWYLoginInfo(new WYLoginInfo(userInfo.getImUserName(),userInfo.getImUserPassword()));
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("loginState",true);
        editor.putString("id",userInfo.getId()==null?"":userInfo.getId());
        editor.putInt("enable",userInfo.getEnable());
        editor.putString("remark",userInfo.getRemark()==null?"":userInfo.getRemark());
        editor.putString("createBy",userInfo.getCreateBy()==null?"":userInfo.getCreateBy());
        editor.putLong("createTime",userInfo.getCreateTime());
        editor.putString("updateBy",userInfo.getUpdateBy()==null?"":userInfo.getUpdateBy());
        editor.putLong("updateTime",userInfo.getUpdateTime());
        editor.putString("keyword",userInfo.getKeyword()==null?"":userInfo.getKeyword());
        editor.putString("userName",userInfo.getUserName()==null?"":userInfo.getUserName());
        editor.putString("phone",userInfo.getPhone() == null?"":userInfo.getPhone());
        editor.putString("address",userInfo.getAddress() ==null?"":userInfo.getAddress());
        editor.putString("token",userInfo.getToken()==null?"":userInfo.getToken());
        editor.putString("slogan",userInfo.getSlogan() == null?"":userInfo.getSlogan());
        editor.putString("imUserName",userInfo.getImUserName()==null?"":userInfo.getImUserName());
        editor.putString("imUserPassword",userInfo.getImUserPassword() == null ?"":userInfo.getImUserPassword());
        editor.putString("photoUrl",userInfo.getPhotoUrl()==null?"":userInfo.getPhotoUrl());
        editor.putInt("sex",userInfo.getSex());
        editor.commit();
        DataState.getInstance().setWYLoginInfo(new WYLoginInfo(userInfo.getImUserName(),userInfo.getImUserPassword()));
    }


    public static void loginOut(){
        DataState.getInstance().setLoginState(false);
        DataState.getInstance().setTempImtoken(null);
        DataState.getInstance().setWYLoginInfo(null);
    }


}
